package com.example.cheeseerpadmin.sys.common;

import com.baomidou.mybatisplus.generator.config.builder.Mapper;
import com.example.cheeseerpadmin.bus.entity.BusCustomer;
import com.example.cheeseerpadmin.bus.entity.BusGoods;
import com.example.cheeseerpadmin.bus.entity.BusProvider;
import com.example.cheeseerpadmin.bus.mapper.BusCustomerMapper;
import com.example.cheeseerpadmin.bus.mapper.BusGoodsMapper;
import com.example.cheeseerpadmin.bus.mapper.BusProviderMapper;
import com.example.cheeseerpadmin.sys.entity.SysDept;
import com.example.cheeseerpadmin.sys.entity.SysUser;
import com.example.cheeseerpadmin.sys.mapper.SysDeptMapper;
import com.example.cheeseerpadmin.sys.mapper.SysUserMapper;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 缓存池：用来集中管理所有的缓存
 */
public class CachePool {
    /**
     * 所有缓存都存放到这个总得缓存容器当中来
     */
    public static volatile Map<String, Object> CACHE_CONTAINER = new HashMap<>();

    /**
     * 根据key来删除对应的缓存对象
     *
     * @param key
     */
    public static void removeById(String key) {
        if (CACHE_CONTAINER.containsKey(key)) {
            CACHE_CONTAINER.remove(key);
        }
    }

    /**
     * 清空所有的缓存
     */
    public static void removeAll() {
        CACHE_CONTAINER.clear();
    }

    /**
     * 同步缓存
     */
    public static void syncData() {
        // 这里因为是一个静态方法，它较之其他的普通方法会提前加载。
        // 而如果我们想要去拿到对应的对象mapper，并且根据mapper来完成缓存的查处的话。
        // 也需要一个方法来提前将mapper放到spring的ioc容器当中供我们使用，

        //部门缓存同步
        SysDeptMapper sysDeptMapper = SpringUtils.getBean(SysDeptMapper.class);
        List<SysDept> sysDeptList = sysDeptMapper.selectList(null);
        for (SysDept sysDept : sysDeptList) {
            CACHE_CONTAINER.put("dept:" + sysDept.getId(), sysDept);
        }

        //用户缓存同步
        SysUserMapper sysUserMapper = SpringUtils.getBean(SysUserMapper.class);
        List<SysUser> sysUserList = sysUserMapper.selectList(null);
        for (SysUser sysUser : sysUserList) {
            CACHE_CONTAINER.put("user:" + sysUser.getId(), sysUser);
        }

        //客户缓存同步
        BusCustomerMapper busCustomerMapper = SpringUtils.getBean(BusCustomerMapper.class);
        List<BusCustomer> busCustomerList = busCustomerMapper.selectList(null);
        for (BusCustomer busCustomer : busCustomerList) {
            CACHE_CONTAINER.put("customer:" + busCustomer.getId(), busCustomer);
        }

        //商品缓存同步
        BusGoodsMapper busGoodsMapper = SpringUtils.getBean(BusGoodsMapper.class);
        List<BusGoods> busGoodsList = busGoodsMapper.selectList(null);
        for (BusGoods busGoods : busGoodsList) {
            CACHE_CONTAINER.put("goods:" + busGoods.getId(), busGoods);
        }

        //供应商缓存同步
        BusProviderMapper busProviderMapper = SpringUtils.getBean(BusProviderMapper.class);
        List<BusProvider> busProviderList = busProviderMapper.selectList(null);
        for (BusProvider busProvider : busProviderList) {
            CACHE_CONTAINER.put("provider:" + busProvider.getId(), busProvider);
        }
    }
}
